/*
 * Created on 2005-7-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sohu.sip.auth.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sohu.peak.util.Logger;
import com.sohu.sip.auth.business.bean.ActorItemBean;
import com.sohu.sip.auth.business.event.ActorItemAddEvent;
import com.sohu.sip.auth.business.event.ActorItemDeleteEvent;
import com.sohu.sip.auth.business.event.ActorItemListEvent;
import com.sohu.sip.auth.business.result.ActorItemAddResult;
import com.sohu.sip.auth.business.result.ActorItemDeleteResult;
import com.sohu.sip.auth.business.result.ActorItemListResult;
import com.sohu.sip.auth.constant.AuthConstant;
import com.sohu.sip.auth.util.ParamUtil;

/**
 * @author luciali
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ActorItemAction extends BaseAction {
	private static Logger log = new Logger();

	public String insertActorItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			ActorItemAddEvent event = new ActorItemAddEvent(ParamUtil.getLong(request, "actorId").longValue(),
					(String[])request.getParameterValues("mid"));
			ActorItemAddResult result = ActorItemBean.getInstance().addActorItems(event);
			request.setAttribute("result", result);

			if (result.getStatus() == 0) {
				response.sendRedirect("actoritem_listLeftActorItem.sip?actorId=" + ParamUtil.getLong(request, "actorId"));
				return null;
			} else {
				return "failure";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "failure";
		}
	}

	public String deleteItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			ActorItemDeleteEvent event = new ActorItemDeleteEvent((String[])request.getParameterValues("mid"));
			ActorItemDeleteResult result = ActorItemBean.getInstance().deleteActorItems(event);
			request.setAttribute("result", result);

			if (result.getStatus() == 0) {
				//response.sendRedirect("actoritem_success.sip");
				return "success";
			} else {
				return "failure";
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return "failure";
		}
	}
	
	public String listActorItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			ActorItemListEvent event = new ActorItemListEvent(ParamUtil.getLong(request, "actorId").longValue(),
					ParamUtil.getInt(request, "pagination.pageNO", 1).intValue(),
					ParamUtil.getInt(request, "pageCount", AuthConstant.COUNT_PERPAGE).intValue());
			ActorItemListResult result = ActorItemBean.getInstance().list(event);
			request.setAttribute("result", result);
			request.setAttribute("pagination", result.getPagination());
			if (result.getStatus() == 0)
				return "listActorItem";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e);
			return "failure";
		}
	}

	public String listActorItem2() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			ActorItemListEvent event = new ActorItemListEvent(
					ParamUtil.getInt(request, "pagination.pageNO", 1).intValue(),
					ParamUtil.getInt(request, "pageCount", AuthConstant.COUNT_PERPAGE).intValue(),
					ParamUtil.getLong(request, "itemId").longValue());
			ActorItemListResult result = ActorItemBean.getInstance().list(event);
			request.setAttribute("result", result);
			request.setAttribute("pagination", result.getPagination());
			if (result.getStatus() == 0)
				return "listActorItem2";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e);
			return "failure";
		}
	}
	
	public String listLeftActorItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			ActorItemListEvent event = new ActorItemListEvent(ParamUtil.getLong(request, "actorId").longValue(),
					ParamUtil.getInt(request, "pagination.pageNO", 1).intValue(),
					ParamUtil.getInt(request, "pageCount", AuthConstant.COUNT_PERPAGE).intValue());
			ActorItemListResult result = ActorItemBean.getInstance().listLeft(event);
			request.setAttribute("result", result);
			request.setAttribute("pagination", result.getPagination());
			if (result.getStatus() == 0)
				return "listLeftActorItem";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e);
			return "failure";
		}
	}

	public String success() {
		return "success";
	}

	public String failure() {
		return "failure";
	}		
}
